﻿Public Class ViewModelParametrerReassurance
    Inherits ViewModelTabSectionBase

    Private _ListeClesProjection As DataView
    Private _ListeClesReassurance As DataView
    Private _ListeModelesReassurance As DataView
    Dim _ModeleReassuranceActif As DataRowView
    Dim _ListeReassurance As Object


    Public ReadOnly Property ListeClesProjection As DataView
        Get

            If _ListeClesProjection Is Nothing Then
                Dim datamanager = New DataManager()
                _ListeClesProjection = New DataView(datamanager.Dataset.ReferenceProjection)
            End If

            Return _ListeClesProjection
        End Get
    End Property

    Public ReadOnly Property ListeClesReassurance As DataView
        Get
            If _ListeClesReassurance Is Nothing Then
                Dim datamanager = New DataManager()
                '      _ListeClesReassurance = datamanager.Dataset.Reassurance.DefaultView.ToTable(True, "Groupe", "Code_Garantie", "Generation").DefaultView
                _ListeClesReassurance = datamanager.Dataset.ReferenceProjection.DefaultView.ToTable(True, "Groupe", "Code_Garantie", "Generation").DefaultView
            End If
            Return _ListeClesReassurance
        End Get
    End Property

    Public ReadOnly Property ListeReassurance As DataView
        Get
            If _ListeReassurance Is Nothing Then
                Dim datamanager = New DataManager()
                _ListeReassurance = New DataView(datamanager.Dataset.Reassurance)
            End If
            Return _ListeReassurance
        End Get
    End Property


    Public ReadOnly Property ListeModelesReassurance As DataView
        Get
            If _ListeModelesReassurance Is Nothing Then
                Dim datamanager = New DataManager()

                If datamanager.Dataset.ModeleReassurance.Count = 0 And _
                    datamanager.Dataset.Reassurance.Count > 0 Then

                    Dim q = From elt In datamanager.Dataset.Reassurance
                            Select Quote_Part_Coass = If(elt.IsQuote_Part_CoassNull(), 0, elt.Quote_Part_Coass), _
                                   Quote_Part_Reass = If(elt.IsQuote_Part_ReassNull(), 0, elt.Quote_Part_Reass), _
                                   Taux_Com_Reass = If(elt.IsTaux_Com_ReassNull(), 0, elt.Taux_Com_Reass), _
                                   Taux_Produit_Financier = If(elt.IsTaux_Produit_FinancierNull(), 0, elt.Taux_Produit_Financier), _
                                   Exclure_SelectionMedicale = If(elt.IsExclure_SelectionMedicaleNull(), 0, elt.Exclure_SelectionMedicale) Distinct

                    For Each elt In q
                        datamanager.Dataset.ModeleReassurance.AddModeleReassuranceRow( _
                             "Modele " & (datamanager.Dataset.ModeleReassurance.Count + 1), _
                             elt.Quote_Part_Coass, elt.Quote_Part_Reass, elt.Taux_Com_Reass, elt.Exclure_SelectionMedicale, elt.Taux_Produit_Financier)
                    Next

                   
                End If
                _ListeModelesReassurance = datamanager.Dataset.ModeleReassurance.DefaultView
            End If
            Return _ListeModelesReassurance
        End Get
    End Property

    Public Property ModeleReassuranceActif As DataRowView
        Get
            Return _ModeleReassuranceActif
        End Get
        Set(value As DataRowView)
            _ModeleReassuranceActif = value

            If value Is Nothing Then Return

            Dim row As DataModel.PmtDataset.ModeleReassuranceRow = value.Row

            Dim datamanager = New DataManager()

            Dim relcols = New String() {"Quote_Part_Coass", "Quote_Part_Reass", "Taux_Com_Reass", "Taux_Produit_Financier", "Exclure_SelectionMedicale"}

            Dim qstr = From elt In relcols
                      Select "(" & elt & "=" & Convert.ToString(row(elt), System.Globalization.CultureInfo.InvariantCulture) & ")"

            ListeReassurance.RowFilter = String.Join(" AND ", qstr.ToArray())

            OnPropertyChanged("ModeleReassuranceActif")
        End Set
    End Property
End Class
